package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author: jie.zhao
 * @create: 2021/01/31 17:43
 */
public class XmlMapperBuilder {

    public void parseConfig(Configuration configuration, InputStream inputStream) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        Element root = document.getRootElement();
        String nameSpace = root.attribute("namespace").getValue();

        // 查询语句
        List<Element> propertyList = root.selectNodes("//select");
        for (Element element:propertyList){
            String id = element.attribute("id").getValue();
            String resultType = element.attribute("resultType").getValue();
            String parameterType = element.attribute("parameterType")==null?"":element.attribute("parameterType").getValue();
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            configuration.getSqlMapperMap().put(nameSpace+"."+id,mappedStatement);
        }

        // 插入语句解析
        List<Element> insertPropertyList = root.selectNodes("//insert");
        for (Element element:insertPropertyList){
            String id = element.attribute("id").getValue();
            String parameterType = element.attribute("parameterType").getValue();
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sql);
            configuration.getSqlMapperMap().put(nameSpace+"."+id,mappedStatement);
        }

        // 删除语句解析
        List<Element> deletePropertyList = root.selectNodes("//delete");
        for (Element element:deletePropertyList){
            String id = element.attribute("id").getValue();
            String parameterType = element.attribute("parameterType").getValue();
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sql);
            configuration.getSqlMapperMap().put(nameSpace+"."+id,mappedStatement);
        }

        // 修改语句解析
        List<Element> updatePropertyList = root.selectNodes("//update");
        for (Element element:updatePropertyList){
            String id = element.attribute("id").getValue();
            String parameterType = element.attribute("parameterType").getValue();
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sql);
            configuration.getSqlMapperMap().put(nameSpace+"."+id,mappedStatement);
        }


    }
}
